package work.wiwf.web.model.sys.bean;

import java.util.ArrayList;
import java.util.List;

import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import work.wiwf.web.model.sys.bean.entity.SysUserEntity;

public class SysUserBean extends SysUserEntity implements UserDetails {

	private static final long serialVersionUID = 1L;

	private List<SimpleGrantedAuthority> authorities;

	private String token;

	public SysUserBean() {
		super();
	}

	public SysUserBean(SysUserEntity entity) {
		super();

		this.userId = entity.getUserId();
		this.name = entity.getName();
		this.memo = entity.getMemo();

		this.username = entity.getUsername();
		this.password = entity.getPassword();

		this.nonLock = entity.getNonLock();
		this.nonExpired = entity.getNonExpired();
		this.nonExpiredC = entity.getNonExpiredC();
		this.isEnabled = entity.getIsEnabled();

		this.creatorId = entity.getCreatorId();
		this.creatorName = entity.getCreatorName();

	}

	public SysUserBean(String username, String password) {
		super();

		this.username = username;
		this.password = password;
	}

	public SysUserBean(String username, String password, String... permissions) {
		super();

		this.username = username;
		this.password = password;

		this.authorities = new ArrayList<>();

		for (String authoritie : permissions) {
			this.authorities.add(new SimpleGrantedAuthority(authoritie));
		}
	}

	@Override
	public List<SimpleGrantedAuthority> getAuthorities() {
		return authorities;
	}

	public void setAuthorities(List<String> permissions) {
		this.authorities = new ArrayList<>();

		for (String authoritie : permissions) {
			this.authorities.add(new SimpleGrantedAuthority(authoritie));
		}
	}

	@Override
	public String getPassword() {
		return password;
	}

	@Override
	public void setPassword(String password) {
		this.password = password;
	}

	@Override
	public String getUsername() {
		return username;
	}

	@Override
	public void setUsername(String username) {
		this.username = username;
	}

	@Override
	public boolean isAccountNonExpired() {
		return nonExpired == 1;
	}

	@Override
	public boolean isAccountNonLocked() {
		return nonLock == 1;
	}

	@Override
	public boolean isCredentialsNonExpired() {
		return nonExpiredC == 1;
	}

	@Override
	public boolean isEnabled() {
		return isEnabled == 1;
	}

	public String getToken() {
		return token;
	}

	public void setToken(String token) {
		this.token = token;
	}
}
